package com.wl.api.controller.system;

import java.util.Objects;
import java.util.function.Predicate;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.RWYException;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.business.CustomerUserGradeDTO;
import com.wl.api.dto.system.SecurityGradePriceDTO;
import com.wl.api.model.system.SecurityGrade;
import com.wl.api.model.system.SecurityGradePrice;
import com.wl.api.service.business.ICustomerUserGradeService;
import com.wl.api.service.system.ISecurityGradePriceService;
import com.wl.api.service.system.ISecurityGradeService;

@RestController
@RequestMapping("/admin/customer/grade")
public class AdminCustomerGradeController extends CommonController {

	@Autowired
	ICustomerUserGradeService userGradeService;
	
	@RequestMapping(value="/batch", method=RequestMethod.POST)
	public JsonObject create(@RequestBody CustomerUserGradeDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", code = "";
		if (validate().test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			initData(param);
			userGradeService.insertOrUpdateCustomerGradeBatch(param);
			json.setData(true);
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	
	@RequestMapping(value="/manage", method = RequestMethod.GET)
	public JsonObject getItemList(CustomerUserGradeDTO param) {
		JsonObject json = JsonObject.getInstance();
		json.setPage(userGradeService.getCustGradePage(param));
		setJsonMessage(json);
		return json;
		
	}
	


	private void initData(CustomerUserGradeDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.buildCreateMsg(user.getId(), user.getUsername()).buildUpdateMsg(user.getId(), user.getUsername());
	} 
	
	private Predicate<CustomerUserGradeDTO> validate() {
		return  s -> CollectionUtils.isEmpty(s.getUserIds()) || s.getGradeId() == null;
				    
	}
}
